﻿package com.projects.xenia.documents_classes;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;

import android.content.ContentValues;
import android.database.Cursor;


import com.projects.xenia.global_modules.MainGlobalModule;

public class Documents_PhotoReport {

	public DocHeader Head;
	public DocLine Line;
		
	public class DocHeader {
		
		public String id;
		public int docnum;
		public Date date_create;
		public Date date_save;
		public int state;
		public int verstamp;
		public String user_id;
		public String employees_id;
		public String customers_id;
		public int planned;
		public Double total_photo;
			
	}
	
	public class DocLine {
		
		public String id;
		public String hdoc_id;		
		public int docline;
		public String photoreportype_id;		
		
	}
	
	private int GetLastDocNumByCurrentDate () {
		
		int DocNum = 0;
		Cursor ScalarResult = MainGlobalModule.DBE.rawQuery("select max(docnum) from xdoc_photoreports_h", null);
		
		if (ScalarResult.getCount()>0) {
			ScalarResult.moveToFirst();
			DocNum = ScalarResult.getInt(0);								
		}
		
		ScalarResult.close();
		
		DocNum++;
		
		return (DocNum);
		
	}	
	
	public boolean OpenDoc (String Id) {

		Head = new DocHeader ();
		Line = new DocLine ();
		
		try {
			
			Cursor DocumentsResult = MainGlobalModule.DBE.rawQuery("select * from xdoc_photoreports_h where id = ?",new String [] {Id});
		
			DocumentsResult.moveToFirst();
			
			Head.id = DocumentsResult.getString(DocumentsResult.getColumnIndex("id"));
			Head.docnum = DocumentsResult.getInt(DocumentsResult.getColumnIndex("docnum"));
		
			Head.date_create = MainGlobalModule.sdf.parse(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_create")));
			Head.date_save = MainGlobalModule.sdf.parse(DocumentsResult.getString(DocumentsResult.getColumnIndex("date_save"))); 
						
		
			Head.state = DocumentsResult.getInt(DocumentsResult.getColumnIndex("state"));		
			Head.verstamp = DocumentsResult.getInt(DocumentsResult.getColumnIndex("verstamp"));
			Head.user_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("user_id"));
			Head.employees_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("employees_id"));		
			Head.customers_id = DocumentsResult.getString(DocumentsResult.getColumnIndex("customers_id"));		
			Head.planned = DocumentsResult.getInt(DocumentsResult.getColumnIndex("planned"));
			
			Head.total_photo = DocumentsResult.getDouble(DocumentsResult.getColumnIndex("total_photo"));
			
			
		} catch (Exception e) {
			
			return false;
			
		} 
		
		return true;	
		
	}	
	
	public boolean NewDoc () {
		
		Head = new DocHeader ();
		Line = new DocLine ();
		
		try {
			
			Head.id = UUID.randomUUID().toString().toUpperCase();
			Head.docnum = GetLastDocNumByCurrentDate ();
			
			Head.date_create = Calendar.getInstance().getTime();
			Head.date_save = Calendar.getInstance().getTime();
			
			Head.state = 1;
			Head.verstamp = 0;
			Head.user_id = MainGlobalModule.SettingsCollection.get("ManagerId");
			Head.employees_id = MainGlobalModule.SettingsCollection.get("ManagerId");
			Head.customers_id = null;		
			Head.planned = 0;
			Head.total_photo = 0.0;
			
			InsertHeaderIntoDB ();
			
			return true;
			
		} catch (Exception e) {
			
			return false;
			
		}	
		
	}

	private boolean InsertHeaderIntoDB () {
		
		try {
			ContentValues InsertValues = new ContentValues ();
			InsertValues.put("id", Head.id);
			InsertValues.put("docnum", Head.docnum);		
			InsertValues.put("date_create", MainGlobalModule.DateToSqlString(Head.date_create));
			InsertValues.put("date_save", MainGlobalModule.DateToSqlString(Head.date_save));					
			InsertValues.put("state", Head.state);
			InsertValues.put("verstamp", Head.verstamp);
			InsertValues.put("user_id", Head.user_id);
			InsertValues.put("employees_id", Head.employees_id);
			InsertValues.put("customers_id", Head.customers_id);
			InsertValues.put("planned", Head.planned);
			InsertValues.put("total_photo", Head.total_photo);
			MainGlobalModule.DBE.insertOrThrow("xdoc_photoreports_h", null, InsertValues);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}
	
	public boolean DeleteDoc () {
		
		try {
			
			Head.state = 3;
			UpdateDoc ();
			
			return true;
			
		} catch (Exception e) {
			
			return false;
			
		}		
		
	}	
	
	public void DeleteDocLineById (String _id) {
		
		MainGlobalModule.DBE.delete("xdoc_photoreports_t", "id=?", new String [] {_id});
		
	}
	
	public boolean UpdateDoc () {
		
			ContentValues UpdateValues = new ContentValues ();
			UpdateValues.put("id", Head.id);
			UpdateValues.put("docnum", Head.docnum);		
			UpdateValues.put("date_create",  MainGlobalModule.DateToSqlString(Head.date_create));
			UpdateValues.put("date_save", MainGlobalModule.DateToSqlString(Head.date_save));					
			UpdateValues.put("state", Head.state);
			UpdateValues.put("verstamp", Head.verstamp);
			UpdateValues.put("user_id", Head.user_id);
			UpdateValues.put("employees_id", Head.employees_id);
			UpdateValues.put("customers_id", Head.customers_id);
			UpdateValues.put("planned", Head.planned);
			UpdateValues.put("total_photo", Head.total_photo);
			
			MainGlobalModule.DBE.update("xdoc_photoreports_h", UpdateValues, "id=?",new String[] {Head.id} );
			
			return true;
		
	}

	public void FinishDoc () {
		
		if (Head.customers_id==null){
		
			MainGlobalModule.DBE.beginTransaction();
			
				MainGlobalModule.DBE.delete("xdoc_photoreports_h", "id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_photoreports_t", "hdoc_id=?", new String []{Head.id});
				MainGlobalModule.DBE.delete("xdoc_photoreports_d", "hdoc_id=?", new String []{Head.id});			
				MainGlobalModule.DBE.delete("xdoc_gpslog", "id=?", new String []{Head.id});
				
			MainGlobalModule.DBE.setTransactionSuccessful();
			MainGlobalModule.DBE.endTransaction();
			
			
		}
		
	}

	public boolean UpdateInsertDocLine () {
				
		Cursor item_id = MainGlobalModule.DBE.query("xdoc_photoreports_t", new String [] {"id"}, "id=?", new String []{Line.id}, null, null, null);
		
		ContentValues InsertUpdateValues = new ContentValues ();
		
		InsertUpdateValues.put("id",Line.id);
		InsertUpdateValues.put("hdoc_id", Line.hdoc_id);
		InsertUpdateValues.put("docline", Line.docline);
		InsertUpdateValues.put("photoreportype_id", Line.photoreportype_id);		
		InsertUpdateValues.put("sended", 0);
		
		if (item_id.getCount()>0) {
			item_id.moveToFirst();
			MainGlobalModule.DBE.update("xdoc_photoreports_t", InsertUpdateValues, "id=?", new String []{Line.id});	
		}
		else
		{			
			MainGlobalModule.DBE.insert("xdoc_photoreports_t", null, InsertUpdateValues);			
		}		
			return (true);			
		
	}	
		
}
